package com.wwwarehouse.taskcenter.fragment.warehouse_reg;

import android.os.Bundle;

import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.fragment.CommonBasePagerFragment;
import com.wwwarehouse.common.tools.ToastUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.bean.ChooseCarriageBean;
import com.wwwarehouse.taskcenter.constant.TaskCenterConstant;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;

import static com.alibaba.fastjson.JSON.parseObject;

/**
 * Created by clannad.shao on 2017/11/20.
 *
 * @author clannad_sk
 * @date 2017/11/20
 */
public class ChooseCarriageFragment extends CommonBasePagerFragment {

    private static final int CHOOSE_CARRIAGE_REQUEST_CODE = 0x44;
    private static final int CHOOSE_CARRIAGE_PAGESIZE = 10;
    private static final int CHOOSE_CARRIAGE_START_PAGE = 1;
    private String searchText;

    @Override
    public void requestDatas() {
        super.requestDatas();
        Bundle bundle = getArguments();
        ((BaseCardDeskActivity) mActivity).hideFilterBt();
        ((BaseCardDeskActivity) mActivity).hideSortBt();
        if (bundle != null) {
        }
        hideConfirmButton();
        searchText = "";
        requestHttp(CHOOSE_CARRIAGE_START_PAGE, CHOOSE_CARRIAGE_PAGESIZE);
    }

    public void requestHttp(int pageNo, int pageSize) {
        Map<String, Object> map = new HashMap<>(3 * 2);
        map.put("pageNo", pageNo);
        map.put("pageSize", pageSize);
        map.put("searchValue", searchText);
        loadData(TaskCenterConstant.CHOOSE_CARRIAGE, map, CHOOSE_CARRIAGE_REQUEST_CODE);
    }

    /**
     * 请求成功回调
     *
     * @param data
     * @param what
     */
    @Override
    public void onLoadSuccess(String data, int what) {
        if (what == CHOOSE_CARRIAGE_REQUEST_CODE) {
            ChooseCarriageBean chooseCarriageBean = parseObject(data, ChooseCarriageBean.class);
            //判断异常情况
            if (chooseCarriageBean == null) {
                mStateLayout.showEmptyView(true);
                return;
            }
            if (chooseCarriageBean.getList() == null || chooseCarriageBean.getList().isEmpty()) {
                showEmptyView(R.drawable.contract_no_datalist, R.string.warehouse_reg_choose_carriage_no_datalist, false, true);
                return;
            }
            Bundle bundle = new Bundle();
            try {
                //需要传递过滤条件
                bundle.putString("searchText", searchText);
                setData(chooseCarriageBean.getTotal(), CHOOSE_CARRIAGE_PAGESIZE, ChooseCarriageItemFragment.class.getName()
                        , bundle, chooseCarriageBean.getList());
            } catch (Exception e) {
                e.printStackTrace();
                ToastUtils.showToast("数据源为空");
            }
        } else {
            //default

        }
    }

    /**
     * requestError
     *
     * @param errorInfo
     */
    @Override
    public void onLoadError(String errorInfo) {
        //异常情况收集处理 TODO
        System.out.println(errorInfo);
    }

    /**
     * stateLayout click to refresh
     */
    @Override
    public void onRefreshListener() {
        requestHttp(CHOOSE_CARRIAGE_START_PAGE, CHOOSE_CARRIAGE_PAGESIZE);
    }

    @Override
    public void searchNoRealTimeClick(String text) {
        super.searchNoRealTimeClick(text);
        searchText = text;
        requestHttp(CHOOSE_CARRIAGE_START_PAGE, CHOOSE_CARRIAGE_PAGESIZE);
    }


    /**
     * navigation 设置标题
     */
    @Override
    public void setTitle() {
        if (peekFragment() instanceof ChooseCarriageFragment) {
            mActivity.setTitle(getString(R.string.warehouse_reg_choose_carriage_title));
        }
    }

    /**
     * destory
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
